home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / DEBUG / OBJTEST / COMMENTS.PAS next >
Pascal/Delphi Source File  |  1996-06-15  |  5KB  |  128 lines

  1. unit Comments;
  2. (*
  3.  
  4. This software is Freeware. Created by Theodore Kahn (tedkahn@interramp.com),
  5. June 1996 for a talk presented at the North Bay Delphi Sig. (That's north of
  6. San Francisco.) Comments welcome. Enjoy!
  7.  
  8. ================================================================================
  9. The only purpose of this unit is store comments. It contains no code.
  10. ================================================================================
  11.  
  12. BACKGROUND
  13. ================================================================================
  14. The TStringList component is extremely useful in many situations. First, it
  15. encapsulates a double-linked list of strings and adds a variety of methods for
  16. manipulating those strings. For example, sorting, insert and delete. This
  17. alone is of value. But, TStringList also provides for the association of an
  18. object with each string in the list. The object cannot only contain data, but
  19. can also include methods to operate on that data. In fact, TStringList is used
  20. in almost every VCL component. (NOTE: In C++ and VB this functionality can be
  21. found in Collections.)
  22.  
  23. This application demonstrates several of these features. It also shows how to
  24. trap events that occur within the object from your program. Finally, the program
  25. makes extensive use of typecasting.
  26.  
  27. The purpose of the object (TMyObjectClass) is to store a list of numbers
  28. (contained in a TMemo) from which a mean can be calculated. Further, every time
  29. the text in the memo changes, the mean is recalculated.
  30.  
  31. The object contains 1) a TMemo, 2) an event that gets fired when the memo
  32. changes, 3) a field to store the mean, 4) a field to indicate if the mean needs
  33. to be recalculated,  5) a read only property to access the mean, and 6) a
  34. method to calculate the mean.
  35.  
  36.  
  37. WHAT THE PROGRAM DOES
  38. ================================================================================
  39.  
  40. "Add New Object to StringList" button
  41. --------------------------------------
  42. When this button is clicked, it 1) creates a new instance of TMyObjectClass,
  43. MyObject, 2) adds an item to the object`MyStringList, and 3) adds an item to
  44. the listbox lstObjectString.
  45.  
  46. The item to MyStringList consists of the text of the textbox edtString and the
  47. object MyObject. The item to lstObjectString is the same text as added to
  48. MyStringList. lstObjectString allows us to access the various objects.
  49.  
  50. "Delete Object" button
  51. ------------------------
  52. When this button is clicked, it 1) removes the association of the object to the
  53. MyStringList, 2) destroys the object, that is, frees its memory, and 3)
  54. removes the item from the lstObjectString.
  55.  
  56. "Calc" button
  57. -------------
  58. This button illustrates the fact that because the method CalcMean is public, it
  59. can be accessed independent of the data in the object. In fact, that's why
  60. CalcMean was made a separate function from GetMean. In this case, it calculates
  61. the mean of the contents of memo1.
  62.  
  63.  
  64. HOW THE MEMO CHANGE EVENT WORKS
  65. ================================================================================
  66. When the text in the memo field changes, the following events occur in the order
  67. shown:
  68.  
  69. 1) Memo.OnChange event is fired, which has been equated to...
  70.  
  71. 2) TMyObject.MyChange method which is executed causing...
  72.  
  73. 3) TMyObject.OnDataChange event to be fired, which has been equated to...
  74.  
  75. 4) TfrmMain.OnDataChange method which is executed, and contains code that...
  76.  
  77. 5) Retrieves the value of the Mean property, which...
  78.  
  79. 6) Causes the TMyObject.GetMean method to execute, which, if this started with
  80.      #2 above...
  81.  
  82. 7) Causes the TMyObject.CalcMean to execute. Otherwise, the current value of
  83.    FMean is returned.
  84.  
  85. See how easy object programing is!
  86.  
  87.  
  88. SUMMARY
  89. ================================================================================
  90. While the amount of effort shown here is not warranted for this small amount of
  91. data and calulations, it can easily be seen that the structure of the program
  92. and object would not have to be changed much, even if the amount of data and
  93. calculations were to be greatly increased.
  94.  
  95. It should also be noted that the program (the Main unit) itself mearly acts as
  96. an interface to the data and calculations (the ObjUnit). In Object-Speak, this
  97. means that the problem and interface domains are kept separate.
  98.  
  99. INSTRUCTIONS
  100. ================================================================================
  101. 1) Enter text into the editbox labeled "String Text."
  102.  
  103. 2) Enter one or more numbers into the memo labeled "Numbers," memo1.
  104.  
  105. 3) Click "Add New Object to StringList." The contents of the memo are copied
  106.    to the middle memo and the mean of the numbers is displayed below.
  107.  
  108. 4) Repeat the above several times using different text and numbers.
  109.  
  110. 5) Select an item from the listbox on the right. The memo associated with that
  111.    object is displayed in the middle memo, along with its mean below.
  112.  
  113. 6) Click "Delete Object." The object associated with the selected item in the
  114.    listbox on the right is deleted.
  115.  
  116. 7) Click "Calc." The mean of the numbers in the left memo are calculated and
  117.    displayed.
  118.  
  119.  
  120. *)
  121.  
  122.  
  123. interface
  124.  
  125. implementation
  126.  
  127. end.
  128.